/**
 * 
 */
package com.swim.natatoriumgoods.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.swim.natatoriumgoods.entity.NatatoriumGoods;
import com.swim.natatoriumgoods.service.NatatoriumGoodsService;
import com.swim.settlementrecord.entity.SettlementRecord;

/**
 * 游泳馆商品Controller
 * @author tql
 * @version 2021-03-14
 */
@Controller
@RequestMapping(value = "${adminPath}/natatoriumgoods/natatoriumGoods")
public class NatatoriumGoodsController extends BaseController {

	@Autowired
	private NatatoriumGoodsService natatoriumGoodsService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public NatatoriumGoods get(String id, boolean isNewRecord) {
		return natatoriumGoodsService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("natatoriumgoods:natatoriumGoods:view")
	@RequestMapping(value = {"list", ""})
	public String list(NatatoriumGoods natatoriumGoods, Model model) {
		model.addAttribute("natatoriumGoods", natatoriumGoods);
		return "swim/natatoriumgoods/natatoriumGoodsList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<NatatoriumGoods> listData(NatatoriumGoods natatoriumGoods, HttpServletRequest request, HttpServletResponse response) {
		Page<NatatoriumGoods> page = natatoriumGoodsService.findPage(new Page<NatatoriumGoods>(request, response), natatoriumGoods); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("natatoriumgoods:natatoriumGoods:view")
	@RequestMapping(value = "form")
	public String form(NatatoriumGoods natatoriumGoods, Model model) {
		model.addAttribute("natatoriumGoods", natatoriumGoods);
		return "swim/natatoriumgoods/natatoriumGoodsForm";
	}

	/**
	 * 保存游泳馆商品
	 */
	@RequiresPermissions("natatoriumgoods:natatoriumGoods:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated NatatoriumGoods natatoriumGoods) {
		natatoriumGoodsService.save(natatoriumGoods);
		return renderResult(Global.TRUE, text("保存游泳馆商品成功！"));
	}
	
	/**
	 * 停用游泳馆商品
	 */
	@RequiresPermissions("natatoriumgoods:natatoriumGoods:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(NatatoriumGoods natatoriumGoods) {
		natatoriumGoods.setStatus(NatatoriumGoods.STATUS_DISABLE);
		natatoriumGoodsService.updateStatus(natatoriumGoods);
		return renderResult(Global.TRUE, text("停用游泳馆商品成功"));
	}
	
	/**
	 * 启用游泳馆商品
	 */
	@RequiresPermissions("natatoriumgoods:natatoriumGoods:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(NatatoriumGoods natatoriumGoods) {
		natatoriumGoods.setStatus(NatatoriumGoods.STATUS_NORMAL);
		natatoriumGoodsService.updateStatus(natatoriumGoods);
		return renderResult(Global.TRUE, text("启用游泳馆商品成功"));
	}
	
	/**
	 * 删除游泳馆商品
	 */
	@RequiresPermissions("natatoriumgoods:natatoriumGoods:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(NatatoriumGoods natatoriumGoods) {
		natatoriumGoodsService.delete(natatoriumGoods);
		return renderResult(Global.TRUE, text("删除游泳馆商品成功！"));
	}
	
	/**
	 * 跳转查看详情页
	 */
	@RequiresPermissions("natatoriumgoods:natatoriumGoods:view")
	@RequestMapping(value = "details")
	public String details(NatatoriumGoods natatoriumGoods, Model model) {
		model.addAttribute("natatoriumGoods", natatoriumGoods);
		return "swim/natatoriumgoods/natatoriumGoodsDetails";
	}
	
	/**
	 * 修改商品状态
	 * @param natatoriumGoods		商品id
	 * @param goodsStatus		 商品状态 1表示已下架 2表示上架中
	 */
	@RequiresPermissions("natatoriumgoods:natatoriumGoods:edit")
	@RequestMapping(value = "updateGoodsStatus")
	@ResponseBody
	public String updateGoodsStatus(String natatoriumGoodsId, Long goodsStatus) {
		NatatoriumGoods natatoriumGoods = natatoriumGoodsService.get(natatoriumGoodsId);
		natatoriumGoods.setGoodsStatus(goodsStatus);
		if (goodsStatus == 1) {// 商品状态 1表示已下架 2表示上架中
			natatoriumGoods.setDownSellTime(new Date());		// 下架时间
		} else {
			natatoriumGoods.setUpSellTime(new Date());		// 上架时间
		}
		natatoriumGoodsService.update(natatoriumGoods);
		return renderResult(Global.TRUE, text("修改修改商品状态成功！"));
	}
	
	/**
	 * 获取详情数据
	 */
	@RequestMapping(value = "detailsData")
	@ResponseBody
	public String detailsData(String natatoriumGoodsId) {
		return renderResult(Global.TRUE, text("获取详情数据成功！"), natatoriumGoodsService.get(natatoriumGoodsId));
	}
	
	//#################################################
	//################ 平台 游泳馆商品列表 ###################
	//#################################################
	
	/**
	 * 查询列表（平台 游泳馆商品列表）
	 */
//	@RequiresPermissions("natatoriumgoods:natatoriumGoods:view")
	@RequestMapping(value = {"platformList", ""})
	public String platformList(NatatoriumGoods natatoriumGoods, Model model) {
		model.addAttribute("natatoriumGoods", natatoriumGoods);
		return "swim/natatoriumgoods/natatoriumGoodsPlatformList";
	}
	
}